<template>
  <div v-loading="loading">
    <el-card>
      <el-col>
        <el-button size="small" type="primary" @click="dataFormSubmitHandle()">{{ $t('save') }}</el-button>
        <el-button size="small" @click="closeTab()">{{ $t('close') }}</el-button>
      </el-col>
      <el-col :span="24" class="form_parent">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" :label-width="$i18n.locale === 'en-US' ? '120px' : '120px'">
          <el-col :span="12">
            <!-- 规则编号 -->
            <el-form-item label="规则编号" prop="ruleCode">
              <!--文本框-->
              <el-input v-model="dataForm.ruleCode" placeholder="规则编号" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 规则名称 -->
            <el-form-item label="规则名称" prop="ruleName">
              <!--文本框-->
              <el-input v-model="dataForm.ruleName" placeholder="规则名称" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 数据库类型 -->
            <el-form-item label="数据库类型" prop="dbType">
              <!--下拉框-->
              <el-select v-model="dataForm.dbType" placeholder="数据库类型" clearable @change="dbTypeChangeEvent">
                <el-option v-for="template in $getDictList('DATABASE_TYPE', true)" :key="template.val" :label="template.label"
                  :value="template.val"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- 表分类 -->
            <el-form-item label="表分类" prop="ruleClass">
              <!--下拉框-->
              <el-select v-model="dataForm.ruleClass" placeholder="表分类" clearable>
                <el-option v-for="template in $getDictList('DPD_RULE_CLASS', true)" :key="template.val" :label="template.label"
                  :value="template.val"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <!-- 备注说明 -->
            <el-form-item label="备注说明" prop="remark">
              <!--文本域-->
              <el-input type="textarea" :rows="3" v-model="dataForm.remark" placeholder="备注说明"></el-input>
            </el-form-item>
          </el-col>

        </el-form>
      </el-col>
    </el-card>
    <child-list ref="childList"></child-list>
  </div>
</template>

<script>
import debounce from 'lodash/debounce'
import mainContent from '@/views/main-content'
import ChildList from './dpdRuleColumns-child'

var content = mainContent

export default {
  data () {
    return {
      timestamp: this.$route.params.timestamp,
      loading: false,
      activeName: '1',
      dataForm: {
        children: [],
        deltList: [],
        id: this.$route.params.id,
        ruleCode: '',
        ruleName: '',
        ruleClass: '',
        remark: '',
        creator: '',
        dbType: '',
        createDate: '',
        updater: '',
        updateDate: ''
      }
    }
  },
  computed: {
    dataRule () {
      return {
        ruleCode: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ruleName: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        ruleClass: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ],
        dbType: [
          { required: true, message: this.$t('validate.required'), trigger: 'blur' }
        ]
      }
    }
  },
  activated () {
    // 通过路由参数pid, 控制列表请求操作
    if (this.$route.params.timestamp !== this.timestamp) {
      this.$refs['dataForm'].resetFields()
    }
    if (this.$route.params.id) {
      this.dataForm.id = this.$route.params.id
      this.loading = true
      this.getInfo()
    }
  },
  components: {
    ChildList
  },
  methods: {
    dbTypeChangeEvent (val) {
      this.refreshFieldTypeList()
    },
    async refreshFieldTypeList () {
      this.$refs.childList.dbType = this.dataForm.dbType
      await this.$refs.childList.getFieldTypeList()
    },
    // 刷新子列表
    async refreshChildList () {
      if (this.dataForm.id) {
        this.$refs.childList.dataForm.ruleId = this.dataForm.id
        await this.refreshFieldTypeList()
        this.$refs.childList.getDataList().then(() => {
          this.$refs.childList.rowDrop()
        })
      }
    },
    // 获取信息
    getInfo () {
      this.$http.get(`/dpd/dpdRule/${this.dataForm.id}`).then(({ data: res }) => {
        this.loading = false
        if (res.code !== 0) {
          return false
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
        this.refreshChildList()
      }).catch(() => { })
    },
    // 关闭 tab
    closeTab () {
      content.methods.tabRemoveHandle(this.$route.name)
      this.$emit('refreshDataList')
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          this.$message({
            message: this.$t('prompt.valid'),
            type: 'error',
            duration: 1500
          })
          return false
        }
        let child = this.$refs.childList
        // if (child.getAllData().length <= 0) {
        //   this.$message({
        //     message: this.$t('prompt.children'),
        //     type: 'error',
        //     duration: 500
        //   })
        //   return false
        // }
        // 子表验证
        child.fullValidEvent().then(res => {
          if (!res) return
          this.dataForm.children = child.getAllData()
          this.dataForm.deltList = child.deltList
          this.$http[!this.dataForm.id ? 'post' : 'put']('/dpd/dpdRule/', this.dataForm).then(({ data: res }) => {
            if (res.code !== 0) {
              return false
            }
            this.$message({
              message: this.$t('prompt.success'),
              type: 'success',
              duration: 500,
              onClose: () => {
                this.closeTab()
              }
            })
          })
        })
      })
    }, 1000, { 'leading': true, 'trailing': false })
  }
}
</script>
